<script setup>
import { Delete, Edit } from '@element-plus/icons-vue'
import ChannelSelect from './components/ChannelSelect.vue'
import { ref } from 'vue'
import { formatTime } from '@/utils/format'
import { artGetListService, artDelService } from '@/api/article.js'
import articleeditco from './components/ArticleEdit.vue'
import { i18n } from "@/i18n/index.js";
const { t } = i18n.global;

const params = ref({
  pagenum: 1,
  pagesize: 5,
  cate_id: '',
  state: ''
})

const loading = ref(false)
const articleList = ref([])
const total = ref(0)

const getArticleList = async () => {
  loading.value = true
  const res = await artGetListService(params.value)
  articleList.value = res.data.data
  total.value = res.data.total
  loading.value = false
  console.log(articleList.value);

}
getArticleList()

const onAddArticle = () => {
  articleEditRef.value.open({})
}
const onEditArticle = (row) => {
  articleEditRef.value.open(row)
}

const onSizeChange = (size) => {
  params.value.pagenum = 1
  params.value.pagesize = size
  getArticleList()
}
const onCurrentChange = (page) => {
  params.value.pagenum = page
  getArticleList()
}

const onSearch = () => {
  params.value.pagenum = 1
  getArticleList()
}

const onReset = () => {
  params.value.pagenum = 1
  params.value.cate_id = ''
  params.value.state = ''
  getArticleList()
}

const articleEditRef = ref()

const onSuccess = (type) => {
  if (type === 'add') {
    const lastPage = Math.ceil((total.value + 1) / params.value.pagesize)
    params.value.pagenum = lastPage
  }
  getArticleList()
}

const onDeleteArticle = async (row) => {
  await ElMessageBox.confirm(t('message.ConfirmDeleteArticle'), t('message.WarmReminder'), {
    type: 'warning',
    confirmButtonText: t('message.Confirm'), // 替换为 i18n 语法
    cancelButtonText: t('message.Cancel')
  })
  await artDelService(row.id)
  ElMessage({ type: 'success', message: t('message.SuccessDelete') })
  getArticleList()
}
</script>

<template>
  <!-- 右侧的抽屉 -->
  <articleeditco ref="articleEditRef" @success="onSuccess"></articleeditco>

  <!-- 第一部分头部 -->
  <page-container :title="$t('message.ArticleManagement')">
    <template #extra>
      <el-button type="primary" @click="onAddArticle">{{ $t('message.PublishArticle') }}</el-button>
    </template>

    <!-- 第二部分 搜索 -->
    <el-form inline>
      <el-form-item :label="$t('message.ArticleCategory')">
        <ChannelSelect v-model="params.cate_id"></ChannelSelect>
      </el-form-item>
      <el-form-item :label="$t('message.PublishStatus')">
        <el-select v-model="params.state" style="width: 150px">
          <el-option :label="$t('message.Published')" value="已发布"></el-option>
          <el-option :label="$t('message.Draft')" value="草稿"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="onSearch" type="primary">{{ $t('message.Search') }}</el-button>
        <el-button @click="onReset">{{ $t('message.Reset') }}</el-button>
      </el-form-item>
    </el-form>

    <!-- 主体部分，是表格 -->
    <el-table v-loading="loading" :data="articleList" style="width: 100%">
      <el-table-column :label="$t('message.ArticleTitle')" width="400">
        <template #default="{ row }">
          <el-link type="primary" :underline="false">{{ row.title }}</el-link>
        </template>
      </el-table-column>
      <el-table-column :label="$t('message.Category')" prop="cate_name"></el-table-column>

      <!-- 时间 -->
      <el-table-column :label="$t('message.PublishTime')">
        <template #default="{ row }">
          {{ formatTime(row.pub_date) }}
        </template>
      </el-table-column>

      <el-table-column :label="$t('message.Status')" prop="state"></el-table-column>

      <el-table-column :label="$t('message.Actions')" width="100">
        <template #default="{ row }">
          <el-button :icon="Edit" circle plain type="primary" @click="onEditArticle(row)"></el-button>
          <el-button :icon="Delete" circle plain type="danger" @click="onDeleteArticle(row)"></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty :description="$t('message.NoData')" />
      </template>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination @size-change="onSizeChange" @current-change="onCurrentChange" :current-page="currentPage4"
      :page-sizes="[2, 3, 4, 5, 10]" :page-size="params.pagesize" layout="total, sizes, prev, pager, next, jumper"
      :total="total" style="margin-top: 20px">
    </el-pagination>
  </page-container>
</template>

<style lang="scss" scoped></style>
